/**
 * Copyright (c) 2014 zhenrongbao.com, Inc. All Rights Reserved
 * @author: jiangshuguang
 * @Date  : 14-12-29
 * @Time  : 下午10:38
 * @brief :
 */

P.createPage({
    _data:{
        pn:1,
        $refresh:null
    },

    _create:function(){
        var Dialog = require('wap_common:widget/ui/dialog/dialog.js').Dialog,
            util = require('wap_common:widget/ui/libs/util.js').util,
            $page = $('.page-gift'),
            $giftContainer = $page.find('.gift-container'),
            dialog = new Dialog({
                title:'使用说明',
                content:'' +
                    '<div style="text-align: left;line-height: 25px">' +
                    '<div>1、请至电脑版购买时使用。</div>' +
                    '<div>2、每次购买最多仅可使用一张红包。</div>' +
                    '</div>'
            });

        this.data({
            util:util,
            $page:$page,
            dialog:dialog,
            $giftContainer:$giftContainer
        });

        this.loadData();
    },

    _init:function(){
        var me = this,
            $page = this.data('$page');

        $page.find('.explain .text').on('touchstart',function(){
            me.data('dialog').show();
        });
    },

    loadData:function(refresh){
        var me = this,
            util = this.data('util'),
            $giftContainer = this.data('$giftContainer'),
            itemTpl = __inline("./item.tmpl");

        $.get("/user/getBonusListApi",{
            pn:me.data('pn'),
            ps:5
        },function(ret,status){
            if(ret.error_no == 0){
                var html = '',
                    list = ret.data.list,
                    $refresh = me.data('$refresh'),
                    pageCount = ret.data.page_count;

                $.each(ret.data.list,function(k,v){
                    html+=itemTpl({item:v,util:util});
                });
                $giftContainer.append(html);
                if($refresh){
                    $refresh.refresh('afterDataLoading','down');
                    list.length ||  $refresh.refresh('disable');
                }else if(pageCount>1){
                    me.createRefresh();
                }
                var pn = me.data('pn');
                me.data('pn',++pn);
            }
        },'json');

    },

    createRefresh:function(){
        var me = this,
            $refresh;
        $refresh = $('.body-layout').css('height',window.innerHeight).refresh({
            load: function (dir, type) {
                me.loadData(this);
            }
        });
        this.data('$refresh',$refresh);
    }
});
